#include "stdafx.h"
#include "caCallFunMessage.h"

caCCallFunMessage::caCCallFunMessage(size_t pFunAddr, DWORD uCallConv,PVOID obj)
{
	m_dParamBaseSize=sizeof(caSCallStack)-sizeof(BYTE);
	m_sCallRFunParam=(caSCallStack*)new BYTE[PARAM_MAX_COUNT*4+m_dParamBaseSize];
	if(m_sCallRFunParam)
	{
		m_parambuf= new strstream((char*)&m_sCallRFunParam->params,PARAM_MAX_COUNT*4);
		if(m_parambuf)
		{
			m_sCallRFunParam->funAddr=pFunAddr;
			m_sCallRFunParam->callConv=uCallConv;
			m_sCallRFunParam->retValue=0;
			m_sCallRFunParam->callobj=obj;
			m_sCallRFunParam->paramSize=0;
		}
		else
		{
			delete m_sCallRFunParam;
			m_sCallRFunParam=NULL;
		}
		
	}
}

caCCallFunMessage::~caCCallFunMessage()
{
	if(m_parambuf) delete m_parambuf;
	if(m_sCallRFunParam) delete m_sCallRFunParam;
}

PVOID caCCallFunMessage::GetPartData()
{
	m_sCallRFunParam->paramSize=GetPartSize();
	return m_sCallRFunParam;
}

DWORD caCCallFunMessage::GetPartSize()
{
	if(m_parambuf)
		return m_dParamBaseSize+m_parambuf->tellp();
	else
		return 0;
}

template<typename _type>
BOOL caCCallFunMessage::PutParam(_type param)
{
	if(m_parambuf)
	{
		m_parambuf->write((char*)&param,sizeof(_type));
		return TRUE;
	}
	return FALSE;
}

template<typename _type>
BOOL caCCallFunMessage::GetParam(_type& param)
{
	if(m_parambuf)
	{
		m_parambuf->read((char*)&param,sizeof(_type));
		return TRUE;
	}
	return FALSE;
}

void caCCallFunMessage::UndoPush()
{
	if(m_parambuf)
	{
		m_parambuf->seekg(0);
		m_parambuf->seekp(0);
	}
}